package supplier

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"go-ddd/internal/common/logs"
	"go-ddd/internal/servers/web/response"
	"go-ddd/internal/supplier/model"
	"net/http"
)

type SupplierHandler struct {
	Supplier SupplierInterface
}

func NewSupplierHandler(handler SupplierInterface) *SupplierHandler {
	return &SupplierHandler{
		Supplier: handler,
	}
}

func (s *SupplierHandler) GetSupplier(c *gin.Context) {
	var err error
	supplierParams := map[string]interface{}{}
	if err = c.ShouldBindJSON(supplierParams); err != nil {
		response.Err(c, http.StatusBadRequest, err.Error())
		return
	}
	paramsBytes, err := json.Marshal(supplierParams)
	if err != nil {
		response.Err(c, http.StatusBadRequest, err.Error())
		return
	}
	s.Supplier.GetSupplierData(string(paramsBytes))
}
func (s *SupplierHandler) GetSupplierName(c *gin.Context) {
	name := c.Query("name")
	supplierName, err := model.NewVerifySupplierName(name)
	if err != nil {
		response.Err(c, http.StatusBadRequest, err.Error())
		return
	}
	logs.Infof("name is : %s", supplierName.Value())
	pluginName, err := s.Supplier.GetSupplierName(supplierName.Value())
	if err != nil {
		response.Err(c, http.StatusBadRequest, err.Error())
		return
	}
	c.JSON(200, gin.H{"pluginName": pluginName})
}
